//
//  RHomePageController.m
//  EPAa
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 李日青. All rights reserved.
//

#import "RHomePageController.h"
#import "CK_MenuViewController.h"
#import "RNetWorkRequestManager.h"
#import "RCollectionViewCell.h"
#import "CKCycleView.h"
#import "RHomeDetailViewController.h"
#import "RRadioListController.h"
#import "RViewController.h"
#import "RTopicController.h"
#import "RGoodController.h"
#import "RRadioListController.h"
@interface RHomePageController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
//ColectionView
@property (nonatomic,strong)UICollectionView *collectionViews;
//数组属性
@property (nonatomic,strong)NSMutableArray *dataSource;
//轮播图属性
@property (nonatomic,strong)CKCycleView *cycleView;
@end

@implementation RHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    [CK_MenuViewController getMenuViewController].menuArray = [@[@"今日 TODAY",@"电台 RADIO",@"话题 TOPICS",@"阅读 READINHG",@"话题 TOPICS",@"音乐 MUSIC",@"良品 PRODUCTS"]mutableCopy];
    [CK_MenuViewController getMenuViewController].oneArray = [@[@"订阅 SUBSCRITION",@"设置 SETTING"]mutableCopy];
    
    self.cycleView = [[CKCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
    self.cycleView.imgNameArray = [@[@"h1.jpeg",@"h2.jpeg",@"h3.jpeg",@"h4.jpeg",@"h5.jpeg",@"h6.jpeg",@"h7.jpeg"] mutableCopy ];
    self.dataSource = [NSMutableArray new];
    [RNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] Method:CK_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            for (NSDictionary *itemDict in dataDict[@"list"]) {
                RHomeModel *model = [RHomeModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.dataSource addObject:model];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionViews reloadData];
 
        });

    }];
    

    //创建collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionViews = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    //设置layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width * 0.3, self.view.frame.size.height *0.25);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    self.collectionViews.delegate = self;
    self.collectionViews.dataSource = self;
    [self.view addSubview:self.collectionViews];
    //注册cell
    [self.collectionViews registerNib:[UINib nibWithNibName:@"RCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    //注册头视图
    [self.collectionViews registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"菜单.png"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeftBarBtnItem:)];
    self.navigationItem.leftBarButtonItem = item;
}
#pragma mark UicollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        return header;
    }
    return nil;
}

#pragma mark - UICOllectionViewDataSouces
//点击跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    RHomeDetailViewController *detailVC = [[RHomeDetailViewController alloc]init];
    RHomeModel *model = self.dataSource[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%ld",model.type];
    [self.navigationController pushViewController:detailVC animated:YES];
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
    
}
//点击抽屉按钮
- (void)didClickLeftBarBtnItem:(UIBarButtonItem *)sender {
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController]hideLeftViewController];
    }else {
        [[CK_MenuViewController getMenuViewController]showLeftViewController];
    }

}


- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title {
    [self.navigationController popToRootViewControllerAnimated:YES];

    switch (index) {
                   case 1:
        {
            RRadioListController *radioVC = [[RRadioListController alloc]init];
            [self.navigationController pushViewController:radioVC animated:YES];
            
        }
            break;
            case 2:
        {
            RTopicController *topicVC = [RTopicController new];
            [self.navigationController pushViewController:topicVC animated:YES];
        }
            break;
            case 6:
        {
            RGoodController *goodVC = [RGoodController new];
            [self.navigationController pushViewController:goodVC animated:YES];
            
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
